package com.yc.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yc.damai.entity.DmProduct;
import com.yc.damai.entity.DmUser;
import com.yc.web.feign.OtherAction;
import com.yc.web.feign.OtherActionImpl;
import com.yc.web.feign.UserAction;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author navy
 * @company 源辰信息
 * @date 2024/9/21
 * @email haijunzhou@hnit.edu.cn
 */
@RestController
public class IndexDo {

    @RequestMapping("index")
    String index(HttpServletRequest request){
        return "index " + request.getServerPort();
    }

    @Autowired
    RestTemplate restTemplate;
    @RequestMapping("other")
    @HystrixCommand(fallbackMethod = "other1")
    String other(){
        return restTemplate.getForObject("http://damai-other/other", String.class);
    }

    String other1(){
        return "RestTemplate 降级 other";
    }

    @RequestMapping("user")
    String user(){
        return restTemplate.getForObject("http://damai-user/user", String.class);
    }

    @Autowired
    OtherAction otherAction;
    @Autowired
    UserAction userAction;

    @RequestMapping("otherByFeign")
    String otherByFeign(){
        return otherAction.other();
    }

    @RequestMapping("userByFeign")
    String userByFeign(){
        return userAction.user();
    }

    @RequestMapping("myhello")
    String myhello(String myname){
        return otherAction.hello(myname);
    }

    @RequestMapping("add")
    int add(@RequestParam int x,@RequestParam int y){
        return otherAction.add(x,y);
    }

    @RequestMapping("age/{num}")
    String age(@PathVariable int num) {
        return otherAction.age(num);
    }

    @RequestMapping("resetPwd")
    DmUser resetPwd(DmUser user,
                    @RequestParam String pwd,
                    @RequestParam String repwd) {
        return otherAction.resetPwd(user,pwd,repwd);
    }
    
    @Autowired
    RabbitTemplate rabbitTemplate;
    
    @RequestMapping("workQueue")
    String workQueue(String data){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("workQueue", data + " " + i);
        }
        return "工作模式消息发送成功!";
    }

    @RequestMapping("exQueue")
    String exQueue(String data, String key, String ex){
        rabbitTemplate.convertAndSend(ex, key, data);
        return "交换机模式消息发送成功!";
    }

    /////////////////////////////////////////////////
    @Autowired
    OtherActionImpl otherActionImpl;
    @RequestMapping("queryHots")
    List<DmProduct> queryHots(){
        List<DmProduct> dmProducts = otherAction.queryHots();
        // TODO 需要设置数据超时失效
        System.out.println("otherActionImpl = " + otherActionImpl);
        System.out.println("otherActionImpl.getRedis() = " + otherActionImpl.getRedis());
        otherActionImpl.getRedis().put("queryHotsCache", dmProducts);
        return dmProducts;
    }

}
